<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/24
 * Time: 14:09
 */
namespace app\api\controller;

use app\api\model\Yzm;
use think\Controller;
use think\Db;
use think\Request;

/**
 * Class SendSms
 * 手机验证码处理类
 *
 * @package app\api\controller
 */
class Send extends Controller
{
    public function _initialize()
    {
        parent::_initialize();
        isset($_SERVER['HTTP_ORIGIN']) ? header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']) : '';
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, authKey, sessionId");
        header("Access-Control-Allow-Origin:*");
        header('Access-Control-Allow-Headers:x-requested-with,content-type,USER_ID,TOKEN');
        header('Content-Type:application/json; charset=utf-8');
    }
    /**
     * function SendSms
     * 发送短信
     *
     * @param string $phone 手机号
     * @return array
     */
    public function SendSms()
    {
        $mobile =(string)input('post.phone');
        $patt='/^1[34578][0-9]{9}$/';
        if(preg_match($patt,$mobile)){
            $CorpID = 'QTTX009389';//$_POST['CorpID']; //用户名
            $Pwd = 'lv112358';//$_POST['Pwd']; //密码
            $Mobile = $mobile;//$_POST['Mobile']; //手机号
            $randnum=rand(100000,999999);//随机码
            $Content = "您的验证码是".$randnum."。如非本人操作请勿泄露给他人。"; //内容
            $ContentS = rawurlencode(mb_convert_encoding($Content, "gb2312", "utf-8"));//短信内容做GB2312转码处理
            //$url = "https://sdk2.028lk.com/sdk2/LinkWS.asmx/BatchSend2?CorpID=".$CorpID."&Pwd=".$Pwd."&Mobile=".$Mobile."&Content=".$ContentS."&Cell=&SendTime=";
            $url = "https://sdk2.028lk.com/sdk2/BatchSend.aspx?CorpID=".$CorpID."&Pwd=".$Pwd."&Mobile=".$Mobile."&Content=".$ContentS."&Cell=&SendTime=";
            $result=file_get_contents($url);
            //$re=simplexml_load_string($result);
            $re = $result;
            if($re==1){
                $ip = request()->ip();
                $mm = 60 * 5;
                $start_time = time();
                $end_time = $start_time+$mm;
                if(!empty($mobile))
                {
                    $sql = Db::name("yzm")
                        ->insert([
                            'code' => $randnum,
                            'create_time' => $start_time,
                            'end_time' => $end_time,
                            'ip' => $ip,
                            'phone' =>$mobile,
                        ]);
                    $data['status'] = 1;
                    $data['msg'] = "m！";
                } else {
                    $data['status'] = 0;
                    $data['msg'] = "发送失败！";
                }
            }elseif ($re==-2){
                $data['status'] = -2;
                $data['msg'] = "网络访问超时，请稍后再试！";

            }elseif($re==-101) {
                $data['status'] = -101;
                $data['msg'] = "调用接口速度太快";
            }else{
                $data['status'] = 0;
                $data['msg'] = "发送失败";
            }
        }else{
            $data['status'] = -3;
            $data['msg'] = "手机号码格式不正确";
        }
        AjaxReturn(1,'success',$data);
    }
    public function tplSend($ch, $data)
    {
        curl_setopt($ch, CURLOPT_URL,
            'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        $error = curl_error($ch);
        //$this->checkErr($result, $error);
        return $result;
    }

    /**
     * function checkCode
     * 检测验证码
     *
     * @param string $code 验证码
     * @param string $mobile 手机号
     * @return array
     */
    public function checkCode($code,$mobile)
    {
        $codeModel = new Yzm();
        $data = $codeModel->where(['code'=>$code,'phone'=>$mobile])->find();
        if(empty($data)){
            //$status = ['code'=>0,'msg'=>'验证码不正确'];
            AjaxReturn(0,'验证码不正确');
        }else{
            $ip = request()->ip();
            if($data['ip']!=$ip){
//                $status = ['code'=>0,'msg'=>'非法请求'];
                AjaxReturn(0,'非法请求');
            }else{
                if($data['create_time']<time() && $data['end_time']>time()){
//                    $status = ['code'=>1,'msg'=>'验证成功'];
                    return true;
                }else{
//                    $status = ['code'=>0,'msg'=>'验证码已过期'];
                    AjaxReturn(0,'验证码已过期');
                }
            }
        }
        //return $status;
    }
}
